package com.sso.action;

import java.util.Collection;
import java.util.Iterator;
import java.util.Set;

import com.sso.dao.SysMenuResourceViewMapper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheManager;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.mgt.eis.SessionDAO;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("test")
public class TestShiro{
	@Autowired
	private SessionDAO sessionDAO;
    @Autowired
    private CacheManager cacheManager;

    @RequestMapping("test1")
    public void test(){
        Cache cache =cacheManager.getCache("com.sso.shiro.RealmManager.authorizationCache");
        Set keys =cache.keys();
        Iterator it =keys.iterator();
        while (it.hasNext()){
            System.out.print(it.next());
        }
    }

	public void testHelloworld() {
		String username="";
		String password="";
	    Subject subject = SecurityUtils.getSubject();  
	    UsernamePasswordToken token = new UsernamePasswordToken(username,password);  
	  
	    try {  
	        //4、登录，即身份验证  
	        subject.login(token);
	    } catch (AuthenticationException e) {  
	        //5、身份验证失败
	    }  
	    //6、退出  
	    subject.logout();  
	}  
	public void test2(){
		Collection<Session> sessions = sessionDAO.getActiveSessions();

		for(Session session:sessions){

			System.out.println("登录ip:"+session.getHost());

			System.out.println("最后操作日期:"+session.getLastAccessTime());

		}
	}
}
